package cn.wildfire.chat.app.extendsion.ui;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Context;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.PowerManager;
import android.os.Vibrator;

import com.sourcemuch.im.caiba.CaiBaChatApp;

import cn.wildfire.chat.app.main.MainActivity;

/**
 * Create by HEKE, 2019/8/24 17:45
 * TODO 推送通知实现对象
 */
public class PushNotifyImpl {

    private MainActivity mainActivity;
    private Context context;
    private String message;
    private int type;
    private String title;

    protected PowerManager.WakeLock wl;

    public PushNotifyImpl(MainActivity mainActivity, String message, int type) {
        this.mainActivity = mainActivity;
        this.message = message;
        this.type = type;
        prepare(false);
    }

    public PushNotifyImpl(MainActivity mainActivity, String message, String title) {
        this.mainActivity = mainActivity;
        this.context = mainActivity;
        this.message = message;
        this.title = title;
        prepare(false);
    }

    /**
     * 仅提示声音
     */
    private void prepareRing() {
        try {
            Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            Ringtone rt = RingtoneManager.getRingtone(context.getApplicationContext(), uri);
            rt.play();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 熄屏操作防护
     */
    protected boolean screenProtect;

    /**
     * 发送通知前亮屏与振动
     *
     * @param tipOnly
     */
    @SuppressLint("InvalidWakeLockTag")
    public void prepare(boolean tipOnly) {
        try {
            /**
             * 点亮手机屏幕
             * */
            wl = ((PowerManager) mainActivity.getSystemService(Context.POWER_SERVICE))
                    .newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP |
                            PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "bright");
            if (!wl.isHeld()) {
                wl.acquire();
            }
            /**
             * 手机振动
             * */
            if (((CaiBaChatApp) mainActivity.getApplicationContext()).isScreenLocked) {
                Vibrator vib = (Vibrator) mainActivity.getSystemService(Service.VIBRATOR_SERVICE);
                vib.vibrate(1000);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (!tipOnly) {
                sendNotify();
            }
            // 设置延迟熄屏
            if (!screenProtect) {
                screenProtect = true;
                CaiBaChatApp.getMainHandler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        screenProtect = false;
                        if (wl.isHeld()) {
                            wl.release();
                        }
                    }
                }, 5000);
            }
        }
    }

    /**
     * TODO 开启通知
     */
    private void sendNotify() {
        try {
            (((CaiBaChatApp) context.getApplicationContext())).caibaBinder.pushNotify(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
